export default function(coder: any) {


  coder['control_if'] = function(block: any) {

    // If/elseif/else condition.
    // var n = 0;
    var code = '', conditionCode;

    conditionCode = coder.valueToCode(block, 'CONDITION',coder.ORDER_NONE) || 'false';

    code = `${coder.indentString()}if( ${conditionCode} ) {\n`
    coder.indent ++
    code+= coder.statementToCode(block, 'SUBSTACK');
    coder.indent --
    code+= `${coder.indentString()}}`

    if (block.getInput('SUBSTACK2')) {
      code += ` else {\n`
      coder.indent ++
      code+= coder.statementToCode(block, 'SUBSTACK2');
      coder.indent --
      code += `${coder.indentString()}}`
    }
    return code + '\n';
  };

  coder['control_if_else'] = coder['control_if'];

  coder['logic_compare'] = function(block: any) {
    var operator = block.getFieldValue('OP');
    var order = (operator == '==' || operator == '!=') ?
        coder.ORDER_EQUALITY : coder.ORDER_RELATIONAL;
    var argument0 = coder.valueToCode(block, 'OPERAND1', order) || '0';
    var argument1 = coder.valueToCode(block, 'OPERAND2', order) || '0';
    var code = argument0 + ' ' + operator + ' ' + argument1;
    return [code, order];
  };

  coder['control_wait'] = function(block:any) {
    var sec = coder.valueToCode(block, 'DURATION', coder.ORDER_ATOMIC) || '0';
    return `${coder.indentString()}await delay(${sec*1000})\n`
  }





  coder['logic_operation'] = function(block: any) {
    // Operations 'and', 'or'.
    var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
    var order = (operator == '&&') ? coder.ORDER_LOGICAL_AND :
        coder.ORDER_LOGICAL_OR;
    var argument0 = coder.valueToCode(block, 'A', order);
    var argument1 = coder.valueToCode(block, 'B', order);
    if (!argument0 && !argument1) {
      // If there are no arguments, then the return value is false.
      argument0 = 'false';
      argument1 = 'false';
    } else {
      // Single missing arguments have no effect on the return value.
      var defaultArgument = (operator == '&&') ? 'true' : 'false';
      if (!argument0) {
        argument0 = defaultArgument;
      }
      if (!argument1) {
        argument1 = defaultArgument;
      }
    }
    var code = argument0 + ' ' + operator + ' ' + argument1;
    return [code, order];
  };

  coder['logic_negate'] = function(block: any) {
    // Negation.
    var order = coder.ORDER_LOGICAL_NOT;
    var argument0 = coder.valueToCode(block, 'BOOL', order) ||
        'true';
    var code = '!' + argument0;
    return [code, order];
  };

  coder['logic_boolean'] = function(block: any) {
    // Boolean values true and false.
    var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
    return [code, coder.ORDER_ATOMIC];
  };

  coder['logic_null'] = function(block: any) {
    // Null data type.
    return ['null', coder.ORDER_ATOMIC];
  };

  coder['logic_ternary'] = function(block: any) {
    // Ternary operator.
    var value_if = coder.valueToCode(block, 'IF',
        coder.ORDER_CONDITIONAL) || 'false';
    var value_then = coder.valueToCode(block, 'THEN',
        coder.ORDER_CONDITIONAL) || 'null';
    var value_else = coder.valueToCode(block, 'ELSE',
        coder.ORDER_CONDITIONAL) || 'null';
    var code = value_if + ' ? ' + value_then + ' : ' + value_else;
    return [code, coder.ORDER_CONDITIONAL];
  };
}